﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;
using BonjourHotel.Core.Configuration;
using System.Web.Caching;

namespace BonjourHotel.Core.Cache
{
    public class CacheManager
    {
        private static readonly System.Web.Caching.Cache _cache;

        public static bool IsEnabled
        {
            get { return ConfigurationManager.IsCacheEnabled; }
        }

        static CacheManager()
        {
            HttpContext context = HttpContext.Current;
            if (context != null)
            {
                _cache = context.Cache;
            }
            else
            {
                _cache = HttpRuntime.Cache;
            }
        }

        public static object Get(string key)
        {
            return _cache[key];
        }

        public static void Clear()
        {
            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                _cache.Remove(enumerator.Key.ToString());
            }
        }

        public static void Max(string key, object obj)
        {
            Max(key, obj, null);
        }

        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (IsEnabled && (obj != null))
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }

        public static void Remove(string key)
        {
            _cache.Remove(key);
        }
    }
}
